from __future__ import unicode_literals
import uuid

from django.utils.encoding import python_2_unicode_compatible
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models


@python_2_unicode_compatible
class MessageManager(models.Manager):

    def get_conversation(self,sender,recipient):
        """用户私信消息的并集"""
        qs_one = self.filter(sender=sender,recipient=recipient).select_related('sender','recipient')  #A给B发送的私信
        qs_two = self.filter(sender=recipient,recipient=sender).select_related('sender','recipient')  #B给A发送的私信
        return qs_one.union(qs_two).order_by('created_at')

    def get_most_recent_conversation(self,recipient):  # recipient登录用户
        """获取最近一次私信互动的用户"""
        try:
            qs_sent = self.filter(sender=recipient).select_related('sender','recipient')  # 当前登录用户发送的消息
            qs_received = self.filter(recipient=recipient).select_related('sender','recipient')  # 当前登录用户接收的消息
            qs = qs_sent.union(qs_received).latest("created_at")  # 最后一条消息
            if qs.sender == recipient:
                # 如果登录用户有发送消息,则返回消息的接收者
                return qs.recipient
            # 否则返回消息的发送者
            return qs.sender
        except self.model.DoesNotExist:
            # 如果模型实例不存在,则返回当前用户
            return get_user_model().objects.get(username=recipient.username)


@python_2_unicode_compatible
class Message(models.Model):
    """用户间私信"""
    uuid_id = models.UUIDField(primary_key=True, default=uuid.uuid4,editable=False)
    sender = models.ForeignKey(settings.AUTH_USER_MODEL,related_name="sent_message",
                               null=True,blank=True,on_delete=models.SET_NULL,verbose_name="发送者")
    recipient = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="received_message",null=True, blank=True,
                                  on_delete=models.SET_NULL, verbose_name="接收者")
    message = models.TextField(null=True,blank=True,verbose_name="内容")
    unread = models.BooleanField(default=True,db_index=True,verbose_name="是否未读")

    created_at = models.DateTimeField(db_index=True,auto_now_add=True,verbose_name="创建时间")

    objects = MessageManager()

    class Meta:
        verbose_name = "私信"
        verbose_name_plural = verbose_name
        ordering = ['-created_at']